//
//  PictureViewController.m
//  LiangYa_BObject
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 YuHao. All rights reserved.
//

#import "PictureViewController.h"
#import "PictureCollectionViewCell.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDKUI.h>

@interface PictureViewController ()<UICollectionViewDataSource ,UICollectionViewDelegate,UIActionSheetDelegate>
@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic ,strong) UISwipeGestureRecognizer *swipeGestureRecognizer;
@property (nonatomic ,assign) BOOL flag;
@property (nonatomic ,strong) UIView *view1;
@property (nonatomic ,strong) UIView *view2;
@property (nonatomic ,strong) UIView *view3;
@property (nonatomic ,strong) UIImageView *imageview;
@property (nonatomic ,strong) UIImageView *imageview1;
@property (nonatomic ,strong) UIButton *button;
@property (nonatomic ,strong) UIButton *button1;
@property (nonatomic ,assign) BOOL flag1;
@end

@implementation PictureViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
     self.flag = YES;
    self.flag1 = YES;
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.bounds.size.width - 10, self.view.bounds.size.height/2);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    
    UICollectionView *collectionview = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    
    collectionview.delegate = self;
    collectionview.dataSource =self;
    collectionview.pagingEnabled = YES;
    collectionview.contentOffset = CGPointMake(_num*ScreenWidth, 0);
    [self.view addSubview:collectionview];
    
    
    [collectionview registerClass:[PictureCollectionViewCell class] forCellWithReuseIdentifier:@"pic"];
    
    self.view1 = [[UIView alloc] init];
    self.view1.frame = CGRectMake(0, 0, ScreenWidth, 64);
    [self.view addSubview:_view1];
    self.view2 = [[UIView alloc] init];
    self.view2.frame = CGRectMake(ScreenWidth - 120, 70, 110, 100*2/3);
    self.view2.layer.cornerRadius = 10;
    self.view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_view2];
    
    
    
    self.imageview = [[UIImageView alloc] init];
    self.imageview.image = [[UIImage imageNamed:@"picture2"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.imageview.frame = CGRectMake(ScreenWidth - 45, 52, 20, 20);
    [self.view addSubview:self.imageview];
    self.view2.hidden = YES;
    self.imageview.hidden = YES;
    


    
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    
    UIImage *image = [UIImage imageNamed:@"picture1.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.button setImage:image forState:UIControlStateNormal];
    self.button.frame = CGRectMake(20, 20, 30, 30);
    [self.view1 addSubview:self.button];
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    
    UIImage *image1 = [UIImage imageNamed:@"picture.png"];
    image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.button1 setImage:image1 forState:UIControlStateNormal];
    self.button1.frame = CGRectMake(ScreenWidth - 50, 20, 30, 30);
    [self.view1 addSubview:self.button1];
    [self.button1 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button11 = [UIButton buttonWithType:UIButtonTypeSystem];
    button11.frame = CGRectMake(10, 0, 90, self.view2.bounds.size.height/2-1);
    [button11 setTitleColor:[UIColor blackColor] forState:normal];
    [self.view2 addSubview:button11];
    [button11 setTitle:@"分享" forState:UIControlStateNormal];
    [button11 addTarget:self action:@selector(button11Action:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *button12 = [UIButton buttonWithType:UIButtonTypeSystem];
    button12.frame = CGRectMake(10, self.view2.bounds.size.height/2, 90, self.view2.bounds.size.height/2-1);
    [self.view2 addSubview:button12];
    [button12 setTitleColor:[UIColor blackColor] forState:normal];
    [button12 setTitle:@"保存本地" forState:UIControlStateNormal];
    [button12 addTarget:self action:@selector(buttonAction12:) forControlEvents:UIControlEventTouchUpInside];
    
   
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    
    longPress.minimumPressDuration = 0.7;   //长按时间
    longPress.allowableMovement = 200;    //长按范围
    
    [collectionview addGestureRecognizer:longPress];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    tap.numberOfTapsRequired = 1;//判定点击次数.
    tap.numberOfTouchesRequired = 1; //判断用几个手指
    
        [collectionview addGestureRecognizer:tap];
    
}

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
    
    if (longPress.state == UIGestureRecognizerStateBegan) {
        
        
        UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                      initWithTitle:@""
                                      delegate:self
                                      cancelButtonTitle:@"取消"
                                      destructiveButtonTitle:@"保存到本地相册"
                                      otherButtonTitles:nil];
        actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        [actionSheet showInView:self.view];
        
        
    }
}




#pragma mark 分享
- (void)button11Action:(UIButton *)btn
{
    
    NSLog(@"%@",self.name);
    NSString *urlString = [self.picUrl objectAtIndex:self.num];
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];
    //创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"分享:<%@>",self.name]
                                     images:@[image]
                                        url:nil
                                      title:@"分享标题"
                                       type:SSDKContentTypeImage];
    [ShareSDK showShareActionSheet:nil
                             items:nil
                       shareParams:shareParams
               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                   switch (state) {
                       case SSDKResponseStateSuccess:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                           break;
                       }
                       case SSDKResponseStateFail:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                               message:[NSString stringWithFormat:@"%@", error]
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                           break;
                       }
                       case SSDKResponseStateCancel:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享已取消"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                           break;
                       }
                       default:
                           break;
                   }
                   
                   
               }];
    
}


- (void)buttonAction12:(UIButton *)btn
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@""
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:@"保存到本地相册"
                                  otherButtonTitles:nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];

}

- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *message = [NSString string];
    if (!error) {
        message = @"成功保存到相册";
    }else
    {
        message = @"保存相片失败";
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                    message:message
                                                   delegate:self
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil];
    [alert show];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        UIImageWriteToSavedPhotosAlbum(self.imageview1.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
    }
    
}



- (void)tapAction:(UITapGestureRecognizer *)tap
{
    
    
    if (_flag == YES) {
        
        
        [UIView animateWithDuration:0.5 animations:^{
            
            if(self.view2.hidden == NO){
                self.flag1 = !self.flag1;
            
            self.view2.hidden = YES;
            self.imageview.hidden = YES;
                self.flag = !self.flag;
            }else{
            self.view1.frame = CGRectMake(0, -64, ScreenWidth, 64);
            }
        }];
        
    
    }else{
      
        [UIView animateWithDuration:0.5 animations:^{
            self.view1.frame = CGRectMake(0, 0, ScreenWidth, 64);
        }];
        
    }self.flag = !self.flag;
}

- (void)buttonAction:(UIButton *)btn
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)buttonAction1:(UIButton *)btn
{
    if (self.flag1 == YES) {
         self.view2.hidden = NO;
         self.imageview.hidden = NO;
    } else {
        self.view2.hidden = YES;
        self.imageview.hidden = YES;
    }
   
    self.flag1 = !self.flag1;
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _picUrl.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PictureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pic" forIndexPath:indexPath];
    NSString *str = [_picUrl objectAtIndex:indexPath.row];
    [cell.myimageview sd_setImageWithURL:[NSURL URLWithString:str]];
    self.imageview1 = cell.myimageview;
    return cell;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
